#!/bin/sh

# defaults
PREFIX='/usr/local/coymon'
COMPS="ui"
QUIET=""

# URLs
export GOOGLE_BASE=http://coymon.googlecode.com/svn

# required components
REQ="base"

# dirs
export CHECKOUTS='./checkout'
export THIRD_PARTY='./third-party'
export BUILDS='./build'
export HERE=`dirname $0`

# additional global vars
export PYTHON=`which python`
export BUILD_PYTHON='False'

usage() {
    echo
    echo "install [--help] [--quiet] [--prefix=path] [--components=list]"
    echo "          [--without=list] [--with-python=path]	"
    echo
    echo " Installs the base CoyMon software and any additional, optional"
    echo " components."
    echo
    echo " Options: "
    echo "  --help              shows usage and quits"
    echo "  --user              enter the name of the Zope admin user"
    echo "  --pass              enter the password for the Zope admin user"
    echo "  --quiet             suppress nonessential output"
    echo "  --clean             remove the 'build' and 'checkout' directories"
    echo "                      from previous builds"
    echo "  --prefix            specify an installation path for CoyMon"
    echo "                      (defaults to /usr/local/coymon)"
    echo "  --components        a comma-separated list of components you wish"
    echo "                      to install; defaults to 'ui,netflow' ('base'"
    echo "                      is always installed); optionally, the word"
    echo "                      'all' may be used for installing everything."
    echo "  --without           a comma-separated list of components you do"
    echo "                      not want installed"
    echo "  --with-python       specify a path to a Python interpreter to use"
    echo
    echo
}

for OPT in $@; do
    case "$OPT" in
    --h* | -h*)
        usage
        exit 0
        ;;
    --user=*)
        export USERNAME=`echo $OPT | sed -e 's/--user\=//'`
        ;;
    --pass=*)
        export PASSWORD=`echo $OPT | sed -e 's/--pass\=//'`
        ;;
    --prefix=*)
        export PREFIX=`echo $OPT | sed -e 's/--prefix\=//'`
        ;;
    --prefix)
        echo "The '--prefix' argument requires an option."
        exit 1
        ;;
    --with-python=*)
        # pop this argument from the arglist, it is not valid to
        # pass this along to the Python configurator.
        shift;
        PYTHON=`echo $OPT | sed -e 's/--with-python\=//'`
        # use eval to do tilde expansion below
        eval "PYTHON='$PYTHON'"
        echo ""
        echo "Using Python interpreter at $PYTHON"
        ;;
    --with-python)
        # in case someone passed in a --with-python without a value,
        # we raise an error instead of passing it along to configure.py
        # (which would raise an inscrutable error were it to receive this
        # option).
        echo "The '--with-python' argument requires an option."
        exit 1
        ;;
    --components=*)
        COMPS=`echo $OPT | sed -e 's/--components=//' | sed -e 's/,/ /'`
        export COMPS
        ;;
    --components)
        echo "The '--components' argument requires an option."
        exit 1
        ;;
    --quiet* | -q*)
        QUIET="true"
        NEWOPTS="$NEWOPTS $OPT"
        ;;
    --clean* | -c)
        rm -rf $CHECKOUTS $THIRD_PARTY $BUILDS
        ;;
    *)
        echo "error: unknown option '$OPT'."
        echo "Try './install --help' instead."
        ;;
    esac
done

# raise option errors
if [[ "$USERNAME" == "" ]]; then
    ERROR='True'
elif [[ "$PASSWORD" == "" ]]; then
    ERROR='True'
fi
# XXX only do this for UI installs...
if [[ "$ERROR" == "True" ]]; then
    echo
    echo "You must provide a user name and password"
    echo "for the CoyMon management interface (Zope)."
    echo
    echo "Aborting ..."
    echo
    exit 1
fi

# check python
TOO_LOW_MINOR=4
TOO_HIGH_MINOR=6
$PYTHON -c "import sys;" >& /dev/null \
    && PYTHON_OK='True' \
    || PYTHON_OK='False'
VERSION=`$PYTHON -c "import sys;print sys.version_info;" \
    | sed -e 's/[(),]//g'`
MAJOR=`echo $VERSION | awk '{print $1}'`
MINOR=`echo $VERSION | awk '{print $2}'`
if [[ $MAJOR != 2 ]]; then
    PYTHON_OK='False'
elif [[ $MINOR < 4 ]]; then
    PYTHON_OK='False'
elif [[ $MINOR > 5 ]]; then
    PYTHON_OK='False'
fi
if [[ "$PYTHON_OK" == "False" ]]; then
    BUILD_PYTHON='True'
    PYTHON=${PREFIX}/bin/python
    export CPPFLAGS="-I${PREFIX}/include"
    export LDFLAGS="-L${PREFIX}/lib"
    echo
    echo "There seems to be a problem with the python"
    echo "you provided, so CoyMon will use its own."
    echo
fi
export PYTHON

# check for existence of perl; we won't install perl, so much quit if not found
PERL_VERSION=`perl --version|head -2|tail -1|grep '5.8'`
if [[ "$PERL_VERSION" == "" ]]; then
    echo
    echo "You do not appear to have the right version"
    echo "of Perl installed on your system. Please install"
    echo "and then re-try the CoyMon installation."
    echo
    echo "Aborting ..."
    echo
    exit 1
fi

# check for GNU sed
SED_VERSION=`sed --version|head -1|grep 'GNU sed'`
if [[ "$SED_VERSION" == "" ]]; then
    echo
    echo "You do not appear to have GNU sed installed"
    echo "on your system. Please install and then"
    echo "re-try the CoyMon installation."
    echo
    echo "Aborting ..."
    echo
    exit 1
fi

# check for other binaries such as wget/curl, svn
DOWNLOADER=`which wget`
if [[ "$DOWNLOADER" == "" ]]; then
    DOWNLOADER=`which curl`
    if [[ "$DOWNLOADER" == "" ]]; then
        echo
        echo "It appears that wget or curl is not installed"
        echo "or it is not in the path. You must have one of"
        echo "these installed to continue."
        echo
        echo "Aborting ..."
        echo
        exit 1
    else
        DOWNLOADER="$DOWNLOADER -O"
    fi
fi
echo "The following will be used to download files: $DOWNLOADER"
export DOWNLOADER

for BIN in "dirname perl svn"; do
    if [[ `which $BIN` == "" ]]; then
        echo
        echo "The installer requires that $BIN be installed on"
        echo "your system. It is either not installed, or not"
        echo "in your PATH."
        echo 
        echo "Aborting ..."
        echo
        exit 1
    fi
done

# check for no python binary and no --with-python option

# check for existence of PREFIX, create if necessary
export PREFIX
if [ ! -e $PREFIX ]; then
    echo "Creating base directory '${PREFIX}' ..."
    mkdir $PREFIX || ERROR=True
    if [ $ERROR == 'True' ]; then
        echo "Could not create directory '$PREFIX'."
        echo
        echo "Aborting ..."
        echo
        exit 1
    fi
elif [[ `find ${PREFIX}/ -mindepth 1` != "" ]]; then
    echo
    echo "There appears to be a previous installation in $PREFIX."
    echo "Please move the contents elsewhere or pass a different"
    echo "---prefix, as we will not risk deleting your data."
    echo 
    echo "Aborting ..."
    echo
    exit 1
fi

# build coymon components list
COMPSLIST="$REQ $COMPS"

# check out the necessary coymon components from svn; we will obtain dependency
# information from files in the component trunks, so we need this first
if [[ "$COMPS" == "ui" ]]; then
    echo
    echo "You did not set any components to be installed."
    echo "Only the default components will be built, and"
    echo "this may not be what you want."
    echo
    echo "Note that if you are setting up a NetFlow collector,"
    echo "then you will want to pass '--components=netflow-clct'."
    echo "See the INSTALL file for details."
    echo
fi

# get the CoyMon components from Google Code; the checkout script is
# responsible for firing off the dependency download script; the download
# script is responsible for executing the third-party installs
./comps-checkout.sh $COMPSLIST

# with dependencies installed, we can now install components
#./comps-install.sh $COMPSLIST
